package cn.com.generaldata.os.service.impl;

import java.io.Serializable;
import java.util.Iterator;

import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;
import org.springframework.beans.factory.annotation.Autowired;

import cn.com.generaldata.os.pojo.ReportDetail;

/**
 * hibernate拦截器
 */
public class HibernateInterceptor extends EmptyInterceptor {

	private static final long serialVersionUID = -7323048002294508011L;

	public enum InterceptorEvent {
		SAVE, DELETE, UPDATE;
	}

	@Autowired
	private BIShowDataService biShowDataService;

	/**
	 * 删除时
	 */
	@Override
	public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
		if (entity instanceof ReportDetail) {
			ReportDetail rd = (ReportDetail) entity;
			biShowDataService.changeData(rd, BIShowDataService.InterceptorEvent.DELETE);
		}
		super.onDelete(entity, id, state, propertyNames, types);
	}

	/**
	 * 刷新时
	 */
	@Override
	public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
			String[] propertyNames, Type[] types) {
		if (entity instanceof ReportDetail) {
			ReportDetail rd = (ReportDetail) entity;
			biShowDataService.changeData(rd, BIShowDataService.InterceptorEvent.UPDATE);
		}

		return super.onFlushDirty(entity, id, currentState, previousState, propertyNames, types);
	}

	@Override
	public void postFlush(Iterator entities) {
		// TODO Auto-generated method stub
		System.out.println("postflush invkked!");
	}

	/**
	 * 保存时
	 */
	//	@Override
	//	public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
	//		if (entity instanceof ReportDetail) {
	//			ReportDetail rd = (ReportDetail) entity;
	//			biShowDataService.changeData(rd, InterceptorEvent.SAVE);
	//		}
	//
	//		return super.onSave(entity, id, state, propertyNames, types);
	//	}

}
